package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/14 15:13
 * @description
 */
public class Code113 {
    private List<Integer> temp = new ArrayList<>();
    private List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        getResult(root, 0, sum);
        return result;
    }

    private void getResult(TreeNode node, int level, int sum) {
        if (node != null) {
            temp.add(level, node.val);
            if (node.left == null && node.right == null) {
                int s = 0;
                for (int i = 0; i <= level; i++) {
                    s += temp.get(i);
                }
                //System.out.println(s);
                if (s == sum) {
                    List<Integer> t = new ArrayList<>();
                    for (int i = 0; i <= level; i++) {
                        t.add(temp.get(i));
                    }
                    result.add(t);
                }
            }
            level++;
            getResult(node.left, level, sum);
            getResult(node.right, level, sum);
        }
    }
}
